﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wicresoft.CustomerAttributes;
using System.ComponentModel.DataAnnotations;
using System.Reflection;

namespace Wicresoft.Entity.AREntity
{
    // [Table("bmc_core_bmc_computersystem")]
    // [ARForm("BMC.CORE:BMC_ComputerSystem")]
    [ARForm("ASB_CMDB_SCCMSync")]
    public class Server
    {

        #region Property
        [AREntryId]
        public string RequestId { get; set; }

        [ARField(400127400, ARDataType.Character)]
        public string DatasetId { get; set; }
        
        [ARField(200000020,ARDataType.Character)]
        public string Name { get; set; }

        [ARField(2, ARDataType.Character)]
        public string Submitter { get; set; }

        [ARField(200000004, ARDataType.Character, AllowModify = true)]
        public string Type { get; set; }

        [ARField(301002900, ARDataType.Character, AllowModify = true)]
        public string OwnerName { get; set; }

        [CsvMapped(Title = "Model")]
        [ARField(240001002, ARDataType.Character, AllowModify = true)]
        public string Model { get; set; }

        [CsvMapped(Title = "Serial Number")]
        [ARField(200000001, ARDataType.Character)]
        public string SerialNumber { get; set; }

        [CsvMapped(Title = "Domain")]
        [ARField(260140117, ARDataType.Character,AllowModify = true)]
        public string Domain { get; set; }

        [CsvMapped(Title = "SMBIOS BIOS Version")]
        [ARField(536871919, ARDataType.Character, AllowModify = true)]
        public string BIOSVersion { get; set; }

        [CsvMapped(Title = "MAC Address")]
        [ARField(536871947, ARDataType.Character)]
        public string MAC { get; set; }

        [CsvMapped(Title = "IP Address")]
        [ARField(536871951, ARDataType.Character,AllowModify = true)]
        public string IPAddress { get; set; }

        public string ServerModel { get; set; }

        [ARField(536871923, ARDataType.Character, AllowModify = true)]
        public string InstallLocation { get; set; }

        [CsvMapped(Title = "Name")]
        [ARField(536871931, ARDataType.Character, AllowModify = true)]
        public string CPUModel { get; set; }

        [ARField(536871939, ARDataType.Character)]
        public string CPUNumber { get; set; }


        [ARField(536871935, ARDataType.Character,AllowModify = true)]
        public string HA { get; set; }

        [ARField(301016000, ARDataType.Character)]
        public string HostName { get; set; }

        [ARField(530054200, ARDataType.Integer)]
        public bool IsVirtual { get; set; }

        [ARField(3, ARDataType.Date)]
        public DateTime CreateDate { get; set; }

        //[CsvMapped(Title = "Size")]
        //[ARField(536871943, ARDataType.Character)]
        public string DiskSize { get; set; }

        [CsvMapped(Title = "Total Physical Memory (MB)")]
        [ARField(536871927, ARDataType.Character, AllowModify = true)]
        public string MemorySize { get; set; }

        [ARField(536871975, ARDataType.Character,AllowModify = true)]
        public string Mask { get; set; }

        [ARField(536871915, ARDataType.Character, AllowModify = true)]
        public string Vmotion { get; set; }

        [ARField(530058400, ARDataType.Character)]
        public string CITagName { get; set; }

        [CsvMapped(Title="ID")]
        [ARField(536872171, ARDataType.Character)]
        public string SCCMId { get; set; }

        [ARField(200000005, ARDataType.Character, AllowModify = false)]
        public string Item { get; set; }

        [ARField(8,ARDataType.Character, AllowModify = true)]
        public string Description { get; set; }

        [ARField(7,ARDataType.Integer)]
        public int Status { get; set; }
        #endregion

        #region CSV
        public Server(string[] data,string [] title)
        {
            if (data == null || title == null)
            {
                throw new ArgumentNullException("csv data cannot be null!");
            }
            foreach (PropertyInfo item in CsvMappedProperties)
            {
                var csvAttr = item.GetCustomAttributes(false).FirstOrDefault(c => c is CsvMappedAttribute) as CsvMappedAttribute;
                if (csvAttr != null)
                {
                    // var name = title[Array.IndexOf(data, csvAttr.Title)];
                    var content = data[Array.IndexOf(title, csvAttr.Title)];
                    item.SetValue(this, content, null);
                }
            }
        }

        private static readonly IEnumerable<PropertyInfo> CsvMappedProperties =
        typeof(Server).GetProperties().Where(p => p.HasAttribute<CsvMappedAttribute>()).ToArray();

        #endregion
    }
}
